package com.foreknow.view;

import com.foreknow.service.impl.RegisterServiceImpl;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Register extends JFrame {

	private JPanel contentPane;
	private JTextField userName;
	private JTextField password;
	private JTextField againPsw;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Register frame = new Register();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Register() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 379, 419);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("用户名：");
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setFont(new Font("楷体", Font.PLAIN, 18));
		label.setBounds(14, 58, 92, 24);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("密码：");
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setFont(new Font("楷体", Font.PLAIN, 18));
		label_1.setBounds(14, 113, 92, 24);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("确认密码：");
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		label_2.setFont(new Font("楷体", Font.PLAIN, 18));
		label_2.setBounds(14, 169, 92, 24);
		contentPane.add(label_2);
		
		userName = new JTextField();
		userName.setBounds(120, 58, 203, 24);
		contentPane.add(userName);
		userName.setColumns(10);

		password = new JTextField();
		password.setColumns(10);
		password.setBounds(120, 115, 203, 25);
		contentPane.add(password);
		
		againPsw = new JTextField();
		againPsw.setColumns(10);
		againPsw.setBounds(120, 169, 203, 25);
		contentPane.add(againPsw);
		
		JButton btnNewButton = new JButton("确认");
		btnNewButton.setFont(new Font("楷体", Font.PLAIN, 20));
		btnNewButton.setBounds(74, 220, 203, 36);
		contentPane.add(btnNewButton);
		JOptionPane jpane = new JOptionPane();
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				RegisterServiceImpl registerService = new RegisterServiceImpl();
				if (password.getText().equals(againPsw.getText())){
					registerService.register(userName.getText(),password.getText());
				}else {
					JOptionPane.showMessageDialog(jpane, "两次输入的密码不同，请重新输入", "提示", JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		
		JButton button = new JButton("取消");
		button.setFont(new Font("楷体", Font.PLAIN, 20));
		button.setBounds(74, 283, 203, 36);
		contentPane.add(button);
		button.addActionListener(new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				Register.this.dispose();
			}
		});

		this.setVisible(true);
	}
}
